/*
 * @Author: zhangjian
 * @Date: 2023-02-14 16:18:27
 * @LastEditTime: 2023-02-16 10:32:30
 * @LastEditors: zhangjian
 * @Description: node服务对接chatGPT
 */
console.log('加载express')
import express from 'express';
import bodyParser from 'body-parser';
import {getGptAnswer,getPictureAnswer} from "./chatGpt.js"
import {getPuzzle,getPrettyPic,getBQ} from "./rollUp.js"
// const getGptAnswer = require('./chatGpt')
const app = express()
const port = 3000
app.use(bodyParser.json());// 添加json解析
app.use(bodyParser.urlencoded({extended: false}));
//设置跨域访问
// app.all('*', function(req, res, next) {
//   res.header("Access-Control-Allow-Origin", "*");
//   res.header("Content-Type", "application/json;charset=utf-8");
//   next();
// });

app.all('*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", req.headers.origin);
  res.header("Access-Control-Allow-Credentials", "true");
  res.header("Access-Control-Allow-Headers", "*");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By",' 3.2.1');
  if(req.method=="OPTIONS") res.send(200);/*让options请求快速返回*/
  else next();
});



app.post('/sendMsg',  async (req, res) => {
    if(req.body.msg){
        try{
            console.log(req.body)
            const  {token} = req.body
            // const {msg,token} = res.body
            // console.log(req.body.token)
            let result = await getGptAnswer(req.body.msg,token)
            result = JSON.parse(result)
            // let result ={"id":"as-x6vb3ij2zh","object":"chat.completion","created":1711089392,"result":"好的，以下是我为您提供的两个字：\n\n美好\n\n这两个字充满了积极向上的情感，表达了对生活的热爱和期待。希望这两个字能够给您带来一些正能量和美好的想象。","is_truncated":false,"need_clear_history":false,"finish_reason":"normal","usage":{"prompt_tokens":2,"completion_tokens":39,"total_tokens":41}}
            //    let result={result:'下班了下班了，我上班时间是：周一至周五 9:00~19:00;其他时间绝对不加班，除非有加班费！！！！'}
            res.send({code:200,data:result})
        }catch(e){
            console.log(e)
            res.send({code:500,msg:e})
        }
        
    }else{
        res.send({code:500,msg:'请输入消息'})
    }
})
// 成语解释的接口地址
app.post('/getPuzzleExplan',  async (req, res) => {
    if(req.body.key){
        try{
            const  {key} = req.body
            let result = await getPuzzle(key)
            result = JSON.parse(result)
            res.send({code:200,data:result?.data[0] ||{}})
        }catch(e){
            console.log(e)
            res.send({code:500,msg:e})
        }
        
    }else{
        res.send({code:500,msg:'请输入消息'})
    }
})
// 福利图片的接口调用
app.post('/getPrettyPic',  async (req, res) => {
        try{
            // console.log('准备发起请求 搜索福利图片')
            let result = await getPrettyPic()
            result = JSON.parse(result)
            console.log('数据返回')
            console.log(result)
            res.send({code:200,data:result ||{}})
        }catch(e){
            console.log(e)
            res.send({code:500,msg:e})
        }
        
 
})
// 获取图片搜索
app.post('/searchPicture',  async (req, res) => {
    if(req.body.msg){
        try{
            console.log(req.body)
            const  {token} = req.body
            // const {msg,token} = res.body
            // console.log(req.body.token)
            let result = await getPictureAnswer(req.body.msg,token)
            result = JSON.parse(result)
            // let result ={"id":"as-x6vb3ij2zh","object":"chat.completion","created":1711089392,"result":"好的，以下是我为您提供的两个字：\n\n美好\n\n这两个字充满了积极向上的情感，表达了对生活的热爱和期待。希望这两个字能够给您带来一些正能量和美好的想象。","is_truncated":false,"need_clear_history":false,"finish_reason":"normal","usage":{"prompt_tokens":2,"completion_tokens":39,"total_tokens":41}}
            //    let result={result:'下班了下班了，我上班时间是：周一至周五 9:00~19:00;其他时间绝对不加班，除非有加班费！！！！'}
            res.send({code:200,data:result})
        }catch(e){
            console.log(e)
            res.send({code:500,msg:e})
        }
        
    }else{
        res.send({code:500,msg:'请输入消息'})
    }
})

// 获取表情包
app.post('/BigPicture',  async (req, res) => {
    if(req.body.key){
        try{
            let result = await  getBQ({key:req.body.key,pageSize:req.body.pageSize || 10}) 
            result=JSON.parse(result)
            res.send({code:200,data:result})
        }catch(e){
            console.log(e)
            res.send({code:500,msg:e})
        }
        
    }else{
        res.send({code:500,msg:'请输入消息'})
    }
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})